package com.it.gw.modules.station.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.StringUtils
import com.it.common.utils.isNotEmpty
import com.it.common.utils.notNullData
import com.it.data.network.rsp.GQStationVo
import com.it.gw.R
import com.it.gw.databinding.ItemGqStationBinding


class GQStationListAdapter(
    var context: Context,
    var listener: (GQStationVo) -> Unit,
    var datas: List<GQStationVo>
) : RecyclerView.Adapter<GQStationListAdapter.ViewHolder>() {
    companion object {
        const val TAG = "GQStationListAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemGqStationBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.STNM.text = StringUtils.getNoNullData(data.STNM)
        holder.binding.ADDRESS.text = StringUtils.getNoNullData(data.ADDRESS)
        holder.binding.STTP.text = StringUtils.getNoNullData(data.STTP)
        holder.binding.CREATETIME.text = "上次运维时间：${StringUtils.getNoNullData(data.CREATETIME)}"
        if(data.QUEDEAL.isNotEmpty()&&data.QUEDEAL?.length==2){
            holder.binding.QUEDEAL.text = "${data.QUEDEAL?.substring(0,1)}\n\n${data.QUEDEAL?.substring(1,2)}"
        }
        else{
            holder.binding.QUEDEAL.text = StringUtils.getNoNullData(data.QUEDEAL)
        }
        if(data.QUEDEAL=="正常"){
            holder.binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_green)
        }
        else{
            holder.binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_yellow)
        }

        holder.binding.root.setOnClickListener {
            listener.invoke(data)
        }

    }


    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemGqStationBinding) : RecyclerView.ViewHolder(binding.root)
}